package com.md.application.service;

import com.google.common.collect.Lists;
import com.md.api.RoleApi;
import com.md.application.assembler.RoleAssembler;
import com.md.base.exception.ServerException;
import com.md.domain.model.po.RolePO;
import com.md.domain.repository.RoleRepository;
import com.md.domain.service.AuthenticationDomainServices;
import com.md.infrastructure.value.ErrorEnum;
import com.md.model.page.PageResponse;
import com.md.model.system.CommonResult;
import com.md.request.LinkApiRequest;
import com.md.request.LinkModuleRequest;
import com.md.request.QueryRolePageRequest;
import com.md.request.QueryRoleRequest;
import com.md.request.QueryUsersApiRequest;
import com.md.request.QueryUsersAppInfoRequest;
import com.md.request.QueryUsersModuleRequest;
import com.md.request.RoleRequest;
import com.md.request.UpdateStatusRequest;
import com.md.response.RoleResponse;
import com.md.response.UsersApiResponse;
import com.md.response.UsersAppInfoResponse;
import com.md.response.UsersModuleResponse;
import com.md.value.RoleApiTypeEnum;
import com.md.value.RoleModuleTypeEnum;
import com.md.value.server.ServerContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 刘骄阳
 * 2024-12-15 1:46
 */
@Service
@RequiredArgsConstructor
public class RoleApplication implements RoleApi {
  private final AuthenticationDomainServices authenticationDomainServices;
  private final RoleRepository roleRepository;

  @Override
  public CommonResult<RoleResponse> create(final RoleRequest request) {
    RoleAssembler.checkCreateRole(request.getName());
    RolePO rolePO = RoleAssembler.buildCreateRolePO(request);
    authenticationDomainServices.createRole(rolePO);
    return CommonResult.success(RoleAssembler.buildRole2Response(rolePO, null));
  }

  @Override
  public CommonResult<RoleResponse> update(final RoleRequest request) {
    RoleAssembler.checkUpdateRole(request.getId(), request.getName());
    RolePO rolePO = RoleAssembler.buildCreateRolePO(request);
    authenticationDomainServices.updateRole(rolePO);
    return CommonResult.success(RoleAssembler.buildRole2Response(rolePO, null));
  }

  @Override
  public CommonResult<RoleResponse> detail(final QueryRoleRequest request) {
    return CommonResult.success(authenticationDomainServices.queryRoleInfo(request.getId()));
  }

  @Override
  public CommonResult<PageResponse<RoleResponse>> list(final QueryRolePageRequest request) {

    return CommonResult.success(authenticationDomainServices.queryRoleList(RoleAssembler.buildQueryRoleBO(request)));
  }

  @Override
  public CommonResult<Void> updateStatus(final UpdateStatusRequest request) {
    roleRepository.updateRoleStatus(request.getRoleId(), request.getStatus());
    return CommonResult.success();
  }

  @Override
  public CommonResult<Void> linkApi(final LinkApiRequest request) {
    // 校验当前用户是否有该模块的权限
    if (!authenticationDomainServices.checkModulePermission(ServerContext.getUserId(),
      Lists.newArrayList(request.getModuleId()))) {
      throw ServerException.build(ErrorEnum.USERS_NOT_PERMISSION);
    }
    authenticationDomainServices.linkRoleApi(request.getRoleId(), request.getAddApiIdList(),
      request.getDelApiIdList(), RoleApiTypeEnum.NORMAL_INSERT, request.getModuleId());
    return CommonResult.success();
  }

  @Override
  public CommonResult<Void> linkModule(final LinkModuleRequest request) {
    List<Long> allModuleIdList = Lists.newArrayList(request.getAddModuleIdList());
    allModuleIdList.addAll(request.getDelModuleIdList());
    // 校验当前用户是否有该模块的权限
    if (!authenticationDomainServices.checkModulePermission(ServerContext.getUserId(),
      allModuleIdList)) {
      throw ServerException.build(ErrorEnum.USERS_NOT_PERMISSION);
    }
    authenticationDomainServices.linkRoleModule(request.getRoleId(), request.getAddModuleIdList(),
      request.getDelModuleIdList(), 0, RoleModuleTypeEnum.NORMAL_INSERT);
    return CommonResult.success();
  }

  @Override
  public CommonResult<PageResponse<UsersModuleResponse>> moduleList(final QueryUsersModuleRequest request) {
    PageResponse<UsersModuleResponse> pageResponse = authenticationDomainServices.moduleList(ServerContext.getUserId(),
      request.getPageNum(), request.getPageSize());
    return CommonResult.success(pageResponse);
  }

  @Override
  public CommonResult<PageResponse<UsersAppInfoResponse>> appInfoList(final QueryUsersAppInfoRequest request) {
    Long moduleId = request.getModuleId();
    RoleAssembler.checkQueryAppInfoList(moduleId);
    PageResponse<UsersAppInfoResponse> pageResponse = authenticationDomainServices.appInfoList(moduleId,
      request.getPageNum(), request.getPageSize());
    return CommonResult.success(pageResponse);
  }

  @Override
  public CommonResult<PageResponse<UsersApiResponse>> apiList(final QueryUsersApiRequest request) {
    RoleAssembler.checkQueryApiList(request.getAppInfoId());
    PageResponse<UsersApiResponse> pageResponse = authenticationDomainServices.apiList(request.getAppInfoId(),
      request.getPageNum(), request.getPageSize());
    return CommonResult.success(pageResponse);
  }
}
